package cn.wolfcode.lww.controller;

import cn.wolfcode.lww.domain.Comment;
import cn.wolfcode.lww.domain.Hotsell;
import cn.wolfcode.lww.domain.Strategy;
import cn.wolfcode.lww.service.CommentService;
import cn.wolfcode.lww.service.impl.CommentServiceImpl;
import cn.wolfcode.lww.service.impl.HotSellServiceImpl;
import cn.wolfcode.lww.service.impl.StrategyServiceImpl;

import cn.wolfcode.lww.util.QueryObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class StrategyController {
    @Autowired
    StrategyServiceImpl strategyService;
    @Autowired
    HotSellServiceImpl hotSellService;
    @Autowired
    CommentServiceImpl commentService;
    @RequestMapping("/getStrategyAll")
    public String getStrategyAll(Model model, QueryObject queryObject,String classify){
        PageInfo<Strategy> strategyPageInfo = strategyService.selectAll(queryObject,classify);
        model.addAttribute("list",strategyPageInfo);
        List<Hotsell> hotsells = hotSellService.selectAll();
        model.addAttribute("hotSellList",hotsells);
        return "strategy/strategy";
    }
    @RequestMapping("selectByPrimaryKey")
    public String selectByPrimaryKey(String id,Model model){
        Strategy strategy = strategyService.selectByPrimaryKey(id);
        model.addAttribute("strategy",strategy);
        List<Hotsell> hotsells = hotSellService.selectAll();
        model.addAttribute("hotsells",hotsells);
        strategyService.updateWatchById(id);
        List<Comment> comments = commentService.selectByArtIdAfter(id);
        model.addAttribute("commentList",comments);
        return "strategy/strategy_content";
    }
    @RequestMapping("/pushStartEgy")
    public String pushStartEgy(String id,Model model){
        if (id==null){
            model.addAttribute("type","insert");
            return "strategy/push";
        }
        Strategy strategy = strategyService.selectByPrimaryKey(id);
        model.addAttribute("typw","updata");
        model.addAttribute("strategy",strategy);
        return "strategy/push";
    }
    @RequestMapping("/insertStrategy")
    public String insertStrategy(Strategy strategy){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern("yyyy-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(date);
        strategy.setWriteTime(format);
        int insert = strategyService.insert(strategy);
        return "redirect:/getStrategyAll";
    }
    @RequestMapping("/updateStrategy")
    public String updateStrategy(Strategy strategy){
        int i = strategyService.updateByPrimaryKey(strategy);
        return "redirect:/getStategyAll";
    }
@RequestMapping("/deleteStrategy")
    public String deleteStrategy(String id, HttpSession session){

    int i = strategyService.deleteByPrimaryKey(id);
    return "redirect:/getStrategyAll";
}
}
